﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using WebHost;

namespace SilverlightShowWCFRIAColl.ViewModels
{
    public class CollectionViewViewModel : ViewModelBase
    {
        private BookDomainContext _context;
        /// <summary>
        /// The Context property
        /// </summary>
        public BookDomainContext Context
        {
            get
            {
                if (_context == null)
                {
                    _context = new BookDomainContext();
                }
                return _context;
            }
            set
            {
                _context = value;
                RaisePropertyChanged("Context");
            }
        }


        public RelayCommand LoadMoreBooks
        {
            get;
            private set;
        }


        public RelayCommand AddBook
        {
            get;
            private set;
        }

        public RelayCommand AddFilter
        {
            get;
            private set;
        }

        public RelayCommand DeleteBook
        {
            get;
            private set;
        }

        public RelayCommand AddSort
        {
            get;
            private set;
        }

        public RelayCommand AddGrouping
        {
            get;
            private set;
        }

        public RelayCommand Refresh
        {
            get;
            private set;
        }

        private ICollectionView CreateView(IEnumerable source)
        {
            CollectionViewSource cvs = new CollectionViewSource();
            cvs.Source = source;
            return cvs.View;
        }

        private ICollectionView _books;
        public ICollectionView Books
        {
            get
            {
                if (this._books == null)
                {
                    this._books = CreateView(this.Context.Books);
                    this._books.Filter = new Predicate<object>(BookCorrespondsToFilter);
                }
                return this._books;
            }
        }

        public bool BookCorrespondsToFilter(object obj)
        {
            Book book = obj as Book;

            if (filterActive)
            {
                return book.Title.Contains("Silverlight");
            }

            return true;
        }

        bool filterActive = false;



        public CollectionViewViewModel()
        {
            InstantiateCommands();

            // load books
            Context.Load<Book>(Context.GetBooksQuery().Take(10));
        }


        private void InstantiateCommands()
        {
            LoadMoreBooks = new RelayCommand(() =>
            {
                Context.Load<Book>(Context.GetBooksQuery());
            });

            AddBook = new RelayCommand(() =>
            {
                Context.Books.Add(new Book() { Author = "Kevin Dockx", ASIN = "123456", Title = "Dummy book" });
            });

            
            DeleteBook = new RelayCommand(() =>
            {
                Context.Books.Remove(Context.Books.FirstOrDefault());
            });

            AddSort = new RelayCommand(() =>
            {
                Books.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending));
            });

            AddFilter = new RelayCommand(() =>
            {
                filterActive = true;
            });

            AddGrouping = new RelayCommand(() =>
            {
                Books.GroupDescriptions.Add(new PropertyGroupDescription("Author"));
            });

            Refresh = new RelayCommand(() =>
            {
                Books.Refresh();
            });
        }


    }
}
